//
//  REMViewController.m
//  BabyAndBaby
//
//  Created by 尹宁 on 16/8/1.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "REMViewController.h"
#import "recommendTopView.h"
#import "getDistrictModel.h"
#import "recommendTableViewCell.h"
#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"
#import "TZImagePickerController.h"
#import "MBProgressHUD.h"
#import "UIViewController+HUD.h"
#import "ShopDetailViewController.h"
#import "CardView.h"
@interface REMViewController ()<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, UIImagePickerControllerDelegate, ASIHTTPRequestDelegate, UIActionSheetDelegate, UINavigationBarDelegate>
{
    NSMutableArray *arr;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) recommendTopView *recommendTop;
@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIView *pickTopView;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) NSString *number;
@property (nonatomic, strong) NSString *area_id;
@property (nonatomic, strong) NSMutableArray *leftArr;
@property (nonatomic, strong) NSMutableArray *rightArr;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) CardView *cardView;

/**
 *  存放图片的数组
 */
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *imageArray1;
@property (nonatomic, strong) NSMutableArray *imageArray2;
@property (nonatomic, strong) ASINetworkQueue *queue;

// 存放图片string数组
@property (nonatomic, strong) NSMutableArray *strArr;
@property (nonatomic, strong) NSMutableArray *strArr1;
@property (nonatomic, strong) NSMutableArray *strArr2;
/**
 *  拍照
 */
@property (nonatomic, strong)  UIImagePickerController *imagePicker;
@property (nonatomic ,strong) NSString *ImageStr;
@property (nonatomic ,strong) NSString *ImageStr1;
@property (nonatomic ,strong) NSString *ImageStr2;
@property (nonatomic, strong) MBProgressHUD *mbView;// 视图加载

@property (nonatomic, strong) NSString *nStr;
@property (nonatomic, strong) NSString *bStr;
@property (nonatomic, strong) NSMutableArray *imgArr;

@property (nonatomic, strong) NSString *frontStr;
@property (nonatomic, strong) NSString *backStr;

@end

@implementation REMViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.leftArr = [NSMutableArray arrayWithObjects:@"商户编号：",@"商户名称：",@"联系人：",@"登录账号（手机号）：",@"详细地址：",@"店主姓名：",@"店主电话：",@"身份证号：",@"证件上传：",@"营业执照：",@"店铺环境：",@"开户银行：",@"银行卡号：",@"开户人姓名：", nil];
    self.rightArr = [NSMutableArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"", nil];
    
    self.strArr = [NSMutableArray array];
    self.strArr1 = [NSMutableArray array];
    self.strArr2 = [NSMutableArray array];
    self.imgArr = [NSMutableArray array];

    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"delete" object:nil] subscribeNext:^(id x) {
        NSNotification *noti = x;
        NSLog(@"%@", noti.userInfo);
        if ([noti.userInfo[@"section"] isEqualToString:@"1"]) {
            [_imageArray removeObjectAtIndex:[noti.userInfo[@"row"] integerValue]];
//            cell.imaArr = _imageArray;
            [_imgArr removeObjectAtIndex:[noti.userInfo[@"row"] integerValue]];

            
            [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
        }
        if ([noti.userInfo[@"section"] isEqualToString:@"2"]) {
            [_imageArray1 removeObjectAtIndex:[noti.userInfo[@"row"] integerValue]];
            [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:4 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];

        }
        if ([noti.userInfo[@"section"] isEqualToString:@"3"]) {
            [_imageArray2 removeObjectAtIndex:[noti.userInfo[@"row"] integerValue]];
            [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:5 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];

        }
//        [_tableView reloadData];
    }];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatTableView];
    
    _listArr = [NSMutableArray array];
//    self.listArr = [NSMutableArray arrayWithObjects:@"商户编号:",@"商户名称:",@"联系人:",@"登陆账号(手机号):",@"详细地址:",@"店主姓名:",@"店主电话:", nil];
    [self getNumberData];
    [self creatRightButton];
}

- (void)getDistrictData
{
    if (_listArr.count == 0) {
        
        YTKNetHandle *api = [[YTKNetHandle alloc] initWithUrl:[NSString stringWithFormat:@"/Merchant/getMerchantArea.html?token=%@",TOKEN] RegisterApiMethod:RequestMethodGet];
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSArray *arr1 = [request.responseBody objectForKey:@"result"];
            NSLog(@"%@", arr1);
            for (NSDictionary *dic in arr1) {
                getDistrictModel *model = [[getDistrictModel alloc] init];
                model.area_id = [dic objectForKey:@"area_id"];
                _area_id = [dic objectForKey:@"area_id"];
                model.name = [dic objectForKey:@"name"];
                [self.listArr addObject:model];
                [_pickView reloadAllComponents];
            }
        } failure:^(YTKBaseRequest *request) {
            
        }];
    }
}

- (void)getNumberData
{
    YTKNetHandle *api = [[YTKNetHandle alloc] initWithUrl:[NSString stringWithFormat:@"/Merchant/getNumber?token=%@",TOKEN] RegisterApiMethod:RequestMethodGet];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dic = [request.responseBody objectForKey:@"result"];
        NSLog(@"%@", dic);
        _number = [dic objectForKey:@"number"];
        NSLog(@"%@",_number);
        recommendTableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        cell.MerchantCodeTextfield.text = [NSString stringWithFormat:@"商户标号：%@", _number];
        [self.rightArr replaceObjectAtIndex:0 withObject:_number];
        
    } failure:^(YTKBaseRequest *request) {
        
    }];
    
}

- (void)getshangchuanData
{
    // 商户编号
    NSString *str1 = self.rightArr[0];

    // 商户名称
    NSString *str2 = self.rightArr[1];

    // 联系人
    NSString *str3 = self.rightArr[2];
    // 登录账号（手机号）
    NSString *str4 = self.rightArr[3];

    // 详细地址
    NSString *str11 = self.rightArr[4];

    // 店主姓名
    NSString *str5 = self.rightArr[5];

    // 店主电话
    NSString *str6 = self.rightArr[6];

    // 身份证号
    NSString *str7 = self.rightArr[7];

    // 开户银行
    NSString *str8 = self.rightArr[11];

    // 银行卡号
    NSString *str9 = self.rightArr[12];

    // 开户人姓名
//    recommendTableViewCell *cell9 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:2]];
//    NSString *str10 = [cell9.MerchantCodeTextfield.text substringFromIndex:6];
//    if (str10 == nil) {
//        str10 = @"";
//    }
    NSString *str10 = self.rightArr[13];

    _ImageStr = @"";
    for (NSString *str in _strArr) {
        _ImageStr = [_ImageStr stringByAppendingString:str];
        _ImageStr = [_ImageStr stringByAppendingString:@","];
    }
    _ImageStr1 = @"";
    for (NSString *str in _strArr1) {
        _ImageStr1 = [_ImageStr1 stringByAppendingString:str];
        _ImageStr1 = [_ImageStr1 stringByAppendingString:@","];
    }
    _ImageStr2 = @"";
    for (NSString *str in _strArr2) {
        _ImageStr2 = [_ImageStr2 stringByAppendingString:str];
        _ImageStr2 = [_ImageStr2 stringByAppendingString:@","];
    }
    NSString *Image = @"";
    if (_ImageStr.length != 0) {
        Image = [_ImageStr substringToIndex:[_ImageStr length] - 1];// 去掉字符串最后的 ，
    }else{
        Image = @"";
    }
    NSString *Image1 = [NSString stringWithFormat:@""];
    if (_ImageStr1.length != 0) {
        Image1 = [_ImageStr1 substringToIndex:[_ImageStr1 length] - 1];// 去掉字符串最后的 ，
    }else{
        Image1 = @"";
    }
    NSString *Image2 = @"";
    if (_ImageStr2.length != 0 ) {
        Image2 = [_ImageStr2 substringToIndex:[_ImageStr2 length] - 1];// 去掉字符串最后的 ，
    }else{
        Image2 = @"";
    }
    if (_frontStr == nil) {
        _frontStr = @"";
    }
    if (_backStr == nil) {
        _backStr = @"";
    }
    NSDictionary *params = @{@"token":TOKEN, @"area_id":[NSString stringWithFormat:@"%ld",[_area_id integerValue]], @"number":str1,@"name_merchant":str2,@"contact_name":str3,@"user_name":str4,@"address": str11,@"name":str5,@"boss_phone":str6,@"card":str7,@"card_img_positive":_frontStr,@"business_licence":Image1,@"imgs":Image2,@"bank":str8,@"bank_num":str9,@"real_name":str10, @"card_img_negative":_backStr};
    YTKNetHandle *api = [[YTKNetHandle alloc] initWithUrl:@"/Merchant/merchantSave.html" paramDic:params RegisterApiMethod:RequestMethodPost];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        if ([request.responseBody[@"error"] integerValue] == 0) {
            
            
            ShopDetailViewController *vc = [[ShopDetailViewController alloc]init];
            NSDictionary *dic = request.responseBody[@"result"];
            vc.str = [NSString stringWithFormat:@"%ld", [dic[@"id"] integerValue]];
            [self.navigationController pushViewController:vc animated:YES];
            
            
        } else {
            NSLog(@"%@", request.responseBody[@"msg"]);
            [self showHint:request.responseBody[@"msg"] yOffset:-300];
            
        }
    } failure:^(YTKBaseRequest *request) {
        
        NSLog(@"%@", request.responseBody[@"msg"]);
        
    }];
}

- (void)creatRightButton
{
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(0, 0, 50, 30);
    [shareButton setTitle:@"上传" forState:UIControlStateNormal];
    shareButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [[shareButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        NSLog(@"点击");
        [self.tableView endEditing:YES];
        
        // 商户编号
        recommendTableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        NSString *str1 = [cell.MerchantCodeTextfield.text substringFromIndex:5];
        // 商户名称
        recommendTableViewCell *cell1 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        NSString *str2 = [cell1.MerchantCodeTextfield.text substringFromIndex:5];
        // 登录账号（手机号）
        recommendTableViewCell *cell3 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        NSString *str4 = [cell3.MerchantCodeTextfield.text substringFromIndex:10];
        if ([_recommendTop.districtLabel.text isEqualToString:@"片区"]) {
            [self showHint:@"请选择片区" yOffset:-300];
        } else if ([str1 isEqualToString:@""]){
            [self showHint:@"请填写必填信息商户编号" yOffset:-300];
        } else if ([str2 isEqualToString:@""]) {
            [self showHint:@"请填写必填信息商户名称" yOffset:-300];
        } else if ([str4 isEqualToString:@""]) {
            [self showHint:@"请填写必填信息登录账号（手机号）" yOffset:-300];
        } else {
            
            [self getshangchuanData];
        }
        
        
    }];
    UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc]initWithCustomView:shareButton];
    self.navigationItem.rightBarButtonItems = @[barButtonItem1];
    
}

- (NSMutableArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (NSMutableArray *)imageArray1
{
    if (!_imageArray1) {
        _imageArray1 = [NSMutableArray array];
    }
    return _imageArray1;
}

- (NSMutableArray *)imageArray2
{
    if (!_imageArray2) {
        _imageArray2 = [NSMutableArray array];
    }
    return _imageArray2;
}

- (UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; //设置image picker的来源,这里设置为摄像头
        _imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        _imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        _imagePicker.allowsEditing = YES; // 允许编辑
        _imagePicker.delegate = self; // 设置代理。检测操作
    }
    return _imagePicker;
    
}

//选择拍照还是从相册选择
- (void)takePhotoOrSelectAlbum
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    [sheet showInView:self.view];
}

#pragma mark -UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }
            break;
        case 1:
        {
            // 打开相册
            TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:3 delegate:self];
            if ([_nStr isEqualToString:@"1"]) {
//                if ([_bStr isEqualToString:@"front"]) {
                    imagePickerVc.maxImagesCount = 1;
//                }
//                if ([_nStr isEqualToString:@"back"]) {
//                    imagePickerVc.maxImagesCount = 1;
//                }
            } else if ([_nStr isEqualToString:@"2"]) {
                imagePickerVc.maxImagesCount = 6 - self.imageArray1.count;
            } else {
                imagePickerVc.maxImagesCount = 6 - self.imageArray2.count;
            }
            imagePickerVc.navigationBar.barTintColor = [UIColor redColor]; //相册选择器的导航栏的颜色
            imagePickerVc.oKButtonTitleColorDisabled = [UIColor redColor]; //选择照片时文字的颜色
            imagePickerVc.oKButtonTitleColorNormal = [UIColor greenColor]; //没有选择照片时文字的颜色
            imagePickerVc.allowPickingVideo = NO;
            // You can get the photos by block, the same as by delegate.
            // 你可以通过block或者代理，来得到用户选择的照片.
            [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets) {
                
                
                
            }];
            [self presentViewController:imagePickerVc animated:YES completion:nil];
            
        }
            break;
        default:
            break;
    }
}

#pragma mark - UIImagePickerController代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image;
    //如果允许编辑则获得编辑后的照片，否则获取原始照片
    if (self.imagePicker.allowsEditing) {
        image = [info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的图片
    }else{
        image = [info objectForKey:UIImagePickerControllerOriginalImage]; //获取原始照片
    }
    NSData *data = UIImageJPEGRepresentation(image, 1.0f);
    [arr addObject:data];
    [self shangchuan];

    if ([_nStr isEqualToString:@"1"]) {
        if (([_bStr isEqualToString:@"front"])) {
            [_imgArr addObject:@"front"];
        }else if ([_bStr isEqualToString:@"back"]){
            
            [_imgArr addObject:@"back"];
            
        }
        [self.imageArray addObject:image];
        
    } else if ([_nStr isEqualToString:@"2"]) {
        [self.imageArray1 addObject:image];
    } else {
        [self.imageArray2 addObject:image];
    }
    NSLog(@"%ld", self.imageArray.count);
    NSLog(@"%ld", self.imageArray1.count);
    NSLog(@"%ld", self.imageArray2.count);
    [_tableView reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets{
    
    // 把选中的图片转成NSData类型(缩略图)
    arr = [NSMutableArray array];
    for (UIImage *image in photos) {
        
        NSData *data = UIImageJPEGRepresentation(image, 1.0f);
        [arr addObject:data];
    }
    
    [self shangchuan];
    //
    //        // 把选中的图片转成NSData类型(全屏图)
    //    NSData *data1 = UIImageJPEGRepresentation([UIImage imageWithCGImage:[[[assets objectAtIndex:0] defaultRepresentation] fullScreenImage]], 0);    [self.imageArray addObjectsFromArray:photos];
    if ([_nStr isEqualToString:@"1"]) {
        if (([_bStr isEqualToString:@"front"])) {
            [_imgArr addObject:@"front"];
        }else if ([_bStr isEqualToString:@"back"]){
        
            [_imgArr addObject:@"back"];

        }
        
        
        [self.imageArray addObjectsFromArray:photos];
        
    } else if ([_nStr isEqualToString:@"2"]) {
        [self.imageArray1 addObjectsFromArray:photos];
    } else {
        [self.imageArray2 addObjectsFromArray:photos];
    }
    NSLog(@"%ld", self.imageArray.count);
    NSLog(@"%ld", self.imageArray1.count);
    NSLog(@"%ld", self.imageArray2.count);
    [_tableView reloadData];
}



- (void)shangchuan{
    //       上传地址
    //    NSURL *url = [NSURL URLWithString:@"http://qyjjlb.dev.hsydev.com/api.php/Public/imgsUpload.html"];
    self.mbView = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbView.labelText = @"正在上传";
    [self.mbView setMode:MBProgressHUDModeIndeterminate];
    [self.mbView show:YES];
    [self.view addSubview:self.mbView];
    NSString *urlStr = @"http://bzbv3.dev.hsydev.com/tui.php/Base/imgsUpload.html";
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    // 数据类型data
    //    [request setPostFormat:ASIMultipartFormDataPostFormat];
    
    for (int i = 0; i < arr.count; i++) {
        [request addData:arr[i] withFileName:[NSString stringWithFormat:@"name%d.jpeg",i] andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"namee%d",i]];
        
    }
    if (!self.queue) {
        self.queue = [[ASINetworkQueue alloc] init];
        self.queue.maxConcurrentOperationCount = 1;
    }
    [request setDelegate:self];
    
    [request setDidFinishSelector:@selector(requestDone:)];
    [request setDidFailSelector:@selector(requestWentWrong:)];
    [self.queue addOperation:request];
    [self.queue go];
    
}

- (void)requestDone:(ASIFormDataRequest *)request
{
    NSData *data = [request responseData];
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if ([_nStr isEqualToString: @"1"]) {
//        [_strArr addObjectsFromArray:result[@"result"]];
        
        if ([_bStr isEqualToString:@"front"]) {
            _frontStr = [result[@"result"] lastObject];
        }else if ([_bStr isEqualToString:@"back"]){
            _backStr = [result[@"result"] lastObject];
        }
        
        
    } else if ([_nStr isEqualToString:@"2"]) {
//        _strArr1 = [result objectForKey:@"result"];
        [_strArr1 addObjectsFromArray:result[@"result"]];

    } else {
//        _strArr2 = [result objectForKey:@"result"];
        [_strArr2 addObjectsFromArray:result[@"result"]];

    }
    NSLog(@"%@", data);
    
    
    
    
    [self.mbView removeFromSuperview];
}
- (void)requestWentWrong:(ASIFormDataRequest *)request
{
    
    
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@"完成");
}


- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _recommendTop.frame.size.height, KSCREENWIDTH, KSCREENHEIGHT  - _recommendTop.bounds.size.height - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"recommendTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableView];
    _tableView.sectionFooterHeight = 0;
    
    self.recommendTop = [[[NSBundle mainBundle] loadNibNamed:@"recommendTopView" owner:nil options:nil] lastObject];
    _recommendTop.frame = CGRectMake(0, 0, KSCREENWIDTH, 33 * KSCREENHEIGHT / 667);
    [[_recommendTop.bigButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self getDistrictData];
        [self creatPickerView:_recommendTop.bigButton];
    }];
    
    [self.view addSubview:_recommendTop];
    _tableView.tableHeaderView =_recommendTop;
    
    
}

#pragma mark - TableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    } else if (section == 1){
        return 6;
    } else {
        return 3;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 44)];
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 11, KSCREENWIDTH, 33)];
        view1.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 65, 14)];
        label.text = @"基本信息";
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = BACKGROUNDCOLOR;
        [view1 addSubview:label];
        [view addSubview:view1];
        return view;
    } else if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 44)];
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 11, KSCREENWIDTH, 33)];
        view1.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 65, 14)];
        label.text = @"验证信息";
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = BACKGROUNDCOLOR;
        [view1 addSubview:label];
        [view addSubview:view1];
        return view;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 44)];
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 11, KSCREENWIDTH, 33)];
        view1.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 65, 14)];
        label.text = @"账户信息";
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = BACKGROUNDCOLOR;
        [view1 addSubview:label];
        [view addSubview:view1];
        return view;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 44;
    } else {
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 44;
    } else if (indexPath.section == 1){
        if (indexPath.row == 3) {
            if (_imageArray.count == 0) {
                return 44;
            } else {
                NSInteger i = _imageArray.count % 3 ? _imageArray.count / 3 + 1: _imageArray.count / 3;
                return 120 * i + 44;
            }
        } else if (indexPath.row == 4){
            if ((_imageArray1.count == 0)) {
                return 44;
            } else {
                NSInteger i = _imageArray1.count % 3 ? _imageArray1.count / 3 + 1: _imageArray1.count / 3;
                return 120 * i + 44;
            }
        } else if (indexPath.row == 5) {
            if ((_imageArray2.count == 0)) {
                return 44;
            } else {
                NSInteger i = _imageArray2.count % 3 ? _imageArray2.count / 3 + 1: _imageArray2.count / 3;
                return 120 * i + 44;
            }
        }
        return 44;
    } else {
        return 44;
    }
}

- (void)creatCardView
{
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 + 20)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.5;
    self.cardView = [[[NSBundle mainBundle] loadNibNamed:@"CardView" owner:nil options:nil] lastObject];
    
    _cardView.frame = CGRectMake(55 * KSCREENWIDTH / 375, 182 * KSCREENHEIGHT / 667, 265 * KSCREENWIDTH / 375, 80 * KSCREENHEIGHT / 667);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [_backView addGestureRecognizer:tap];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        
        [_backView removeFromSuperview];
        [_cardView removeFromSuperview];
        
    }];
    
    
    [[_cardView.frontCardButton
      rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        _bStr = @"front";
        
        NSInteger i = 0;
        for (NSString *str in _imgArr) {
            if ([str isEqualToString:@"front"]) {
                i++;
                [self showHint:@"您已添加过正面" yOffset:-300];
            }
        }
        if (i == 0) {
            
            [self takePhotoOrSelectAlbum];
            [_backView removeFromSuperview];
            [_cardView removeFromSuperview];
        }else{
            [_backView removeFromSuperview];
            [_cardView removeFromSuperview];
        }
    }];
    [[_cardView.backCardButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        _bStr = @"back";
        NSInteger i = 0;
        for (NSString *str in _imgArr) {
            if ([str isEqualToString:@"back"]) {
                i++;
                [self showHint:@"您已添加过反面" yOffset:-300];
            }
        }
        if (i == 0) {
            
            [self takePhotoOrSelectAlbum];
            [_backView removeFromSuperview];
            [_cardView removeFromSuperview];
        }else{
            [_backView removeFromSuperview];
            [_cardView removeFromSuperview];
        }
    }];
    [self.view addSubview:_backView];
    [self.view addSubview:_cardView];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    recommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if (indexPath.section == 0) {
        
        cell.MerchantCodeTextfield.text = [NSString stringWithFormat:@"%@%@",self.leftArr[indexPath.row], self.rightArr[indexPath.row]];
        cell.collectionView.hidden = YES;
        cell.addButton.hidden = YES;
        cell.MerchantCodeTextfield.enabled = YES;
        if (indexPath.row == 3) {
            cell.MerchantCodeTextfield.keyboardType = UIKeyboardTypePhonePad;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 1) {
            cell.MerchantCodeTextfield.keyboardType = UIKeyboardTypePhonePad;
        }
        cell.MerchantCodeTextfield.enabled = YES;
        cell.MerchantCodeTextfield.text = [NSString stringWithFormat:@"%@%@",self.leftArr[indexPath.row + 5], self.rightArr[indexPath.row + 5]];
        if (indexPath.row == 3) {
            cell.addButton.hidden = NO;
            cell.imaArr = _imageArray;
            cell.MerchantCodeTextfield.enabled = NO;
            cell.str = [NSString stringWithFormat:@"%ld", indexPath.row - 2];
            [[[cell.addButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal]subscribeNext:^(id x) {
                _nStr = @"1";
                [self creatCardView];
            }];
        }
        if (indexPath.row == 4 || indexPath.row == 5) {
            cell.addButton.hidden = NO;
             if (indexPath.row == 4) {
                cell.imaArr = _imageArray1;
                cell.MerchantCodeTextfield.enabled = NO;

            }
            else if (indexPath.row == 5) {
                cell.imaArr = _imageArray2;
                cell.MerchantCodeTextfield.enabled = NO;

            } else {
                cell.collectionView.hidden = YES;
            }
            cell.str = [NSString stringWithFormat:@"%ld", indexPath.row - 2];

            [[[cell.addButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
               
                [self takePhotoOrSelectAlbum];
//                if (indexPath.row == 3) {
//                    _nStr = @"1";
//                }
                if (indexPath.row == 4) {
                    _nStr = @"2";
                }
                if (indexPath.row == 5) {
                    _nStr = @"3";
                }
                
            }];
        }
//            else {
//            cell.addButton.hidden = YES;
//        }
    }else if (indexPath.section == 2){
        cell.MerchantCodeTextfield.text = [NSString stringWithFormat:@"%@%@",self.leftArr[indexPath.row + 11], self.rightArr[indexPath.row + 11]];
        cell.collectionView.hidden = YES;
        cell.addButton.hidden = YES;

    }
    [[[cell.MerchantCodeTextfield rac_signalForControlEvents:UIControlEventEditingDidBegin] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
       
        NSArray *arr1 = [cell.MerchantCodeTextfield.text componentsSeparatedByString:@"："];
        NSString *str = [arr1 lastObject];
        if (str != nil) {
            cell.MerchantCodeTextfield.text = str;
        }
    }];
    [[[cell.MerchantCodeTextfield rac_signalForControlEvents:UIControlEventEditingDidEnd] takeUntil:cell.rac_prepareForReuseSignal]subscribeNext:^(id x) {
        

            if (indexPath.section == 0) {
                [self.rightArr replaceObjectAtIndex:indexPath.row withObject:cell.MerchantCodeTextfield.text];
                cell.MerchantCodeTextfield.text = [NSString stringWithFormat:@"%@%@",self.leftArr[indexPath.row], self.rightArr[indexPath.row]];
            }
            if (indexPath.section == 1) {
                [self.rightArr replaceObjectAtIndex:indexPath.row + 5 withObject:cell.MerchantCodeTextfield.text];
                cell.MerchantCodeTextfield.text = [NSString stringWithFormat:@"%@%@",self.leftArr[indexPath.row + 5], self.rightArr[indexPath.row + 5]];

            }
            if (indexPath.section == 2) {
                [self.rightArr replaceObjectAtIndex:indexPath.row + 5 + 6 withObject:cell.MerchantCodeTextfield.text];
                cell.MerchantCodeTextfield.text = [NSString stringWithFormat:@"%@%@",self.leftArr[indexPath.row + 11], self.rightArr[indexPath.row + 11]];

        }
    }];

    return cell;
 
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    
    
}


#warning 创建PickerView
- (void)creatPickerView:(UIButton *)btn
{
    if (_pickView == nil) {
        
        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 150, KSCREENWIDTH, 150)];
        _pickView.frame = CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144);
        _pickView.delegate = self;
        _pickView.dataSource = self;
        [self.view addSubview:_pickView];
        _pickView.backgroundColor = [UIColor huiseColor];
        
        self.pickTopView = [[UIView alloc]initWithFrame:CGRectMake(0, _pickView.frame.origin.y - 30, KSCREENWIDTH, 30)];
        [self.view addSubview:_pickTopView];
        _pickTopView.backgroundColor = HUIBACKGROUNDCOLOR;
        
    }else{
        [self.view addSubview:_pickView];
        [self.view addSubview:_pickTopView];
    }
    UIButton *done = [UIButton buttonWithType:UIButtonTypeSystem];
    [done setTitle:@"Done" forState:UIControlStateNormal];
    done.frame = CGRectMake(KSCREENWIDTH - 60, 0, 40, 30);
    done.tintColor = [UIColor blackColor];
    [_pickTopView addSubview:done];
    [[done rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_pickView removeFromSuperview];
        [_pickTopView removeFromSuperview];
        
        _recommendTop.districtLabel.text = _str;
    }];
    
}
#warning PickerView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return _listArr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    getDistrictModel *model =_listArr[row];
    _str = model.name;
    NSLog(@"%@", _str);
    return _str;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    getDistrictModel *model =_listArr[row];
    _str = model.name;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
